package root.electronclass.electronclass.home.widget;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import root.electronclass.electronclass.common.base.BasicAdapter;
import root.electronclass.electronclass.common.base.BindingHolder;
import root.electronclass.electronclass.common.util.HeadUtil;
import root.electronclass.electronclass.mvp.GlobalParameter;
import root.electronclass.electronclass.mvp.base.BaseResponse;
import root.electronclass.electronclass.mvp.entity.HomeGoodInfoH;
import root.electronclass.electronclass.mvp.entity.MoralStudentDto;
import root.electronclass.electronclass.mvp.rest.RestManager;
import root.electronclass.electronclass.databinding.HomeGoodInfoBinding;
import root.electronclass.electronclass.databinding.HomeItemGoodBinding;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HomeGoodView {
    private static final String TAG = "HomeExcellentView";

    private Context context;
    private HomeGoodInfoBinding binding;

    private String curType;

    private List<String> typeList;
    {
        typeList = new ArrayList<>();
        typeList.add("校园");
        typeList.add("竞赛");
    }


    public HomeGoodView(Context context, ViewGroup view) {
        this.binding = HomeGoodInfoBinding.bind(view);
        this.context = context;
        init();
    }

    private BasicAdapter<HomeGoodInfoH, HomeItemGoodBinding> adapter = new BasicAdapter<HomeGoodInfoH, HomeItemGoodBinding>() {
        @Override
        public void onBindViewHolder(@NonNull BindingHolder<HomeItemGoodBinding> holder, @Nullable HomeGoodInfoH item) {
            holder.getBinding().tvStudentName.setText(item.getName());
            holder.getBinding().tvDate.setText(item.getModifyTime());
            holder.getBinding().tvScore.setText(String.valueOf(item.getScore()));
            if (item.getFacePhotoFormat() != null && item.getFacePhotoFormat().size() > 0) {
                holder.getBinding().imgHeader.setImageURI(item.getFacePhotoFormat().get(0).getUrl());
            } else {
                holder.getBinding().imgHeader.setActualImageResource(HeadUtil.getHeadPic(item.getGender() == 1 ? 3 : 2));
            }
        }

        @Override
        public void onBindViewHolder(@NonNull BindingHolder<HomeItemGoodBinding> holder, int position) {
            super.onBindViewHolder(holder, position);
        }
    };

    private void init() {
        binding.rvStudentList.setVisibility(View.GONE);
        binding.tabSelector.initData(typeList);
        binding.tabSelector.setOnTabChangedListener(new TabIndicator.OnTabChangedListener() {
            @Override
            public void onChanged(int index) {
                request(String.valueOf(index + 1));
            }
        });
        request("1");
    }
    public void refresh() {
        request(curType);
    }

    public void request(String type) {
        curType = type;
        long id = GlobalParameter.getEcardInfo().getClassId();
        long sid = GlobalParameter.getEcardInfo().getSchoolId();
        MoralStudentDto dto = new MoralStudentDto();
        dto.setDepartId(String.valueOf(id));
        dto.setSchoolId(String.valueOf(sid));
        dto.setType(type);
        RestManager.getInstance().initRest().goodStudent(dto).enqueue(new Callback<BaseResponse<List<HomeGoodInfoH>>>() {
            @Override
            public void onResponse(Call<BaseResponse<List<HomeGoodInfoH>>> call, Response<BaseResponse<List<HomeGoodInfoH>>> response) {
                if (!TextUtils.equals(type, curType)) {
                    return;
                }
                if (response.isSuccessful()) {
                    binding.rvStudentList.setVisibility(View.VISIBLE);
                    LinearLayoutManager manager = new LinearLayoutManager(context, RecyclerView.VERTICAL, false);
                    binding.rvStudentList.setLayoutManager(manager);
                    adapter.replaceAll(response.body().getRows());
                    binding.rvStudentList.setAdapter(adapter);
                }
            }

            @Override
            public void onFailure(Call<BaseResponse<List<HomeGoodInfoH>>> call, Throwable t) {

            }
        });
    }


}
